<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\Admin\ActivityPerRedPacket\CreateRequest;
use App\Http\Requests\Admin\ActivityPerRedPacket\UpdateRequest;
use App\Models\Admin\ActivityPerRedPacket;
use App\Models\Admin\ActivityPerRedPacketLog;
use Illuminate\Http\Request;
use Style;

class ActivityPerRedPacketController extends CommonController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $red_packet = ActivityPerRedPacket::select('id','name','address','contact','mobile','telephone','receive_num','use_num',
            'download','money','account_balance','created_at')
            ->orderBy('created_at','asc')
            ->paginate(20);
        return Style::view('admin.toll.activity_per_red_packet.index',compact('red_packet'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return Style::view('admin.toll.activity_per_red_packet.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(CreateRequest $request)
    {
        $telephone = $request->only('area_code','phone','ext');
        $input = $request->only('name','address','contact','mobile', 'money','account_balance');
        $input['telephone'] = $telephone['area_code'].'-'.$telephone['phone'].'-'.$telephone['ext'];
        $input['telephone'] =trim($input['telephone'],'-');
        $input['created_at'] = date("Y-m-d H:i:s",time());
        $result = ActivityPerRedPacket::create($input);
        if (count($result)>0){
            return redirect('admin/activity_per_red_packet');
        }
        return back()->with('errors',"红包添加失败，请稍后重试！");
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $redPacket = ActivityPerRedPacket::where('id',$id)->first();
        if (!empty($redPacket->telephone)){
            $telephone = explode('-',$redPacket->telephone);
            $redPacket->area_code = $telephone[0];
            $redPacket->phone = $telephone[1];
            if (isset($telephone[2])){
                $redPacket->ext = $telephone[2];
            }else{
                $redPacket->ext = null;
            }

        }
        return Style::view('admin.toll.activity_per_red_packet.edit',compact('redPacket'));

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateRequest $request, $id)
    {
        $telephone = $request->only('area_code','phone','ext');
        $input = $request->only('name','address','contact','mobile', 'money','account_balance','receive_num','use_num',
            'download');
        $input['telephone'] = $telephone['area_code'].'-'.$telephone['phone'].'-'.$telephone['ext'];
        $input['telephone'] =trim($input['telephone'],'-');
        $input['updated_at'] = date("Y-m-d H:i:s",time());
        $result = ActivityPerRedPacket::where('id',$id)->update($input);
        if (count($result)>0){
            return redirect('admin/activity_per_red_packet');
        }
        return back()->with('errors',"红包添加失败，请稍后重试！");
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $result = ActivityPerRedPacket::where('id',$id)->delete();
        if (count($result) > 0){
            return response()->json(['status'=>0,'msg'=>'红包删除成功！']);
        }
        return response()->json(['status'=>1,'msg'=>'红包删除失败，请稍后重试！']);
    }

    public function detail($id)
    {
        $log = ActivityPerRedPacketLog::where('shop_id',$id)
                ->orderBy('receive_at','desc')
                ->paginate(20);
        $red_packet = ActivityPerRedPacket::where('id',$id)->first();
        if (!empty($red_packet->telephone)){
            $telephone = explode('-',$red_packet->telephone);
            $red_packet->area_code = $telephone[0];
            $red_packet->phone = $telephone[1];
            if (isset($telephone[2])){
                $red_packet->ext = $telephone[2];
            }else{
                $red_packet->ext = null;
            }

        }
        return Style::view('admin.toll.activity_per_red_packet.detail',compact('log','red_packet'));
    }
}
